$(function () {
    var loading = weui.loading('loading', {
        className: 'custom-classname'
    });
    var gid = Common.fn.getUrlParam('gid');
    if (!gid) {
        weui.alert('未找到当前商品，请重新尝试');
        location.href = 'mine.html';
    }
    var result = Model.goods.proxyGoodsDetail({
        goodsId: gid
    });
    if (!sessionStorage.getItem('hmdj_stopError')) {
        sessionStorage.setItem('hmdj_stopError', true);
    } else {
        if (result.code) {
            Common.fn.formatOutput(result);
            loading.hide();
            return false;
        }
    }
    // 显示商品图片
    var imageObjs = [],
        urls = [],
        clientWidth = document.body.clientWidth * 0.3;
    for (var i = 0; i < result.data.images.length; i++) {
        var item = '<img class="goods-images-item" data-id="' + i + '" src="' + result.data.images[i].image + '">';
        $('.goods-detail-thumb-item').eq(i).append(item);
        urls.push(result.data.images[i].image);
        // 获取图片高度
        var thisWidth = parseInt($('.goods-detail-thumb-item img').eq(i).css('width')),
            thisHeight = parseInt($('.goods-detail-thumb-item img').eq(i).css('height')),
            present = (clientWidth / thisWidth) * 1.25;
        $('.goods-detail-thumb-item img').eq(i).css('height', thisHeight * present + 'px');
        $('.goods-detail-thumb-item img').eq(i).css('width', thisWidth * present + 'px');
    }
    for(var i = 0;i < urls.length;i++) {
        imageObjs['imageObj_' + i] = { urls: urls, current: urls[i] }
    }
    // 图片插件绑定
    $(document).on('click', '.goods-images-item', function() {
        var id = $(this).attr('data-id');
        previewImage.start(imageObjs['imageObj_' + id])
	});
    // 关注按钮改变
    if (result.data.goods.attention == true) {
        // 已关注
        $('.attention-btn').text('已关注');
        $('.attention-btn').attr('data-state', 1);
    } else {
        $('.attention-btn').text('关注');
        $('.attention-btn').attr('data-state', 0);
    }
    // 添加数据
    $('.portrait-img').attr('src', result.data.bussiness.photo);
    $('.vendor-level').text('v' + result.data.bussiness.grade);
    $('h3').text(result.data.bussiness.nickname);
    $('.score').text(result.data.bussiness.credits);
    $('.bond').text(result.data.bussiness.bond);
    result.data.bussiness.auth == true ? $('.auth').text('已认证') : $('.auth').text('未认证');
    $('.goods-detail').text(result.data.goods.content);
    $('.topPrice').text(result.data.goods.topPrice);
    $('.order-count').text(result.data.goods.join_count);
    $('.vendor-date').text(result.data.goods.createTime);
    $('.praiseNumber').text(result.data.goods.praiseNumber);
    $('.attention-btn').attr('data-id', result.data.bussiness.businessId);
    $('.m-footer-buy-btn').attr('data-id', result.data.goods.goodsId);
    $('.portrait-block').attr('href', 'shop_detail.html?id=' + result.data.bussiness.businessId);
    var sendState = result.data.goods.postage;
    sendState ? $('.sendState').text('包邮') : $('.sendState').text('不包邮');
    // 判断商品是否被点赞
    if (result.data.goods.praise) {
        // 已点赞
        $('.like-icon').attr('src', '../img/icon/like_1.png');
    }
    // 判断商品是否被代理
    if (result.data.goods.user_proxy) {
        // 被代理
        $('.m-footer-buy-btn').text('已代理');
        $('.m-footer-buy-btn').css('background', '#ccc');
    } else {
        $('.m-footer-buy-btn').on('click', function () {
            var gid = $(this).attr('data-id');
            var result = Model.goods.proxyGoods({
                goodsId: gid
            });
            if (result.code) {
                Common.fn.formatOutput(result);
                loading.hide();
                return false;
            }
            weui.alert(result.data, function () {
                location.reload();
            });
        });
    }
    loading.hide();
});

// 关注按钮
$(document).on('click', '.attention-btn', function () {
    var id = $(this).attr('data-id'),
        state = $(this).attr('data-state');
    if (state == 1) {
        // 已关注
        weui.confirm(
            "真的要取消关注吗？",
            function () {
                // 取消关注
                result = Model.business.attendBusiness({
                    businessId: id
                });
                if (result.code) {
                    alert(result.message);
                    location.reload();
                    return false;
                }
                location.reload();
            },
            function () {}
        );
    } else {
        // 关注店铺
        var loading = weui.loading('loading', {
            className: 'custom-classname'
        });
        result = Model.business.attendBusiness({
            businessId: id
        });
        if (result.code) {
            Common.fn.formatOutput(result);
            loading.hide();
            return false;
        }
        loading.hide();
        location.reload();
    }
});

// 点赞按钮
$(document).on('click', '.like-icon', function () {
    var gid = Common.fn.getUrlParam('gid'),
        result = Model.goods.collectGoods({
            goodId: gid
        });
    if (result.code) {
        Common.fn.formatOutput(result);
        return false;
    }
    location.reload();
});

// 分享
$(document).on('click', '.share-btn', function () {
    weui.alert('请点击右上角菜单 -> 发送给朋友或分享到朋友圈 进行分享');
});

// 点击头像跳转店铺
/* $(document).on('click', '.portrait-block', function () {
    var id = $(this).attr('data-id');
    location.href = 'shop_detail.html?id=' + id;
}); */

// 点击图片放大
/* $(document).on('click', '.goods-images-item', function () {
    var url = $(this).attr('src');
    var gallery = weui.gallery(url, {
        className: 'custom-classname',
        onDelete: function () {
            gallery.hide(function () {
                console.log('`gallery` has been hidden');
            });
        }
    });
}); */